package com.ruoyi.security.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.security.domain.AfRule;
import com.ruoyi.security.mapper.AfRuleMapper;
import com.ruoyi.security.service.IAfRuleService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 法规制度Service业务层处理
 *
 * @author cjie
 * @date 2021-07-24
 */
@Service
public class AfRuleServiceImpl extends LinkFileServiceImpl implements IAfRuleService {
    @Autowired
    private AfRuleMapper afRuleMapper;

    /**
     * 查询法规制度
     *
     * @param id 法规制度ID
     * @return 法规制度
     */
    @Override
    public AfRule selectAfRuleById(String id) {
        AfRule afRule = afRuleMapper.selectAfRuleById(id);
        afRule.setFileList(getFileList(id));
        return afRule;
    }

    /**
     * 查询法规制度列表
     *
     * @param afRule 法规制度
     * @return 法规制度
     */
    @Override
    public List<AfRule> selectAfRuleList(AfRule afRule) {
        return afRuleMapper.selectAfRuleList(afRule);
    }

    /**
     * 新增法规制度
     *
     * @param afRule 法规制度
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertAfRule(AfRule afRule) {
        afRule.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        afRule.setCreateTime(DateUtils.getNowDate());
        afRule.setCreateBy(SecurityUtils.getUsername());

        linkFile(afRule.getFileIdList(), afRule.getId());
        return afRuleMapper.insertAfRule(afRule);
    }

    /**
     * 修改法规制度
     *
     * @param afRule 法规制度
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateAfRule(AfRule afRule) {
        afRule.setUpdateTime(DateUtils.getNowDate());
        afRule.setUpdateBy(SecurityUtils.getUsername());

        linkFile(afRule.getFileIdList(), afRule.getId());
        return afRuleMapper.updateAfRule(afRule);
    }

    /**
     * 批量删除法规制度
     *
     * @param ids 需要删除的法规制度ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteAfRuleByIds(String[] ids) {
        deleteLinkFile(ids);
        return afRuleMapper.deleteAfRuleByIds(ids);
    }

    /**
     * 删除法规制度信息
     *
     * @param id 法规制度ID
     * @return 结果
     */
    @Override
    public int deleteAfRuleById(String id) {
        return afRuleMapper.deleteAfRuleById(id);
    }
}
